<template>
  <view class="content">
   <view class="content-header"><image src="https://small.qaqavr.com/img/zhongrui/bg.png" mode=""></image></view>
    <view class="content-form">
      <u--form labelWidth="100px" labelPosition="left" :model="form" :rules="rules" ref="formRef">
        <u-form-item label="姓名" borderBottom ref="item1"><u--input v-model="form.jobSeekerBySelfDto.realName" border="none"></u--input></u-form-item>
        <u-form-item label="电话" borderBottom ref="item1"><u--input v-model="form.jobSeekerBySelfDto.telPhone" type="number" border="none"></u--input></u-form-item>
        <u-form-item label="身份证号" borderBottom ref="item1"><u--input v-model="form.jobSeekerBySelfDto.idCard" type="idcard" border="none"></u--input></u-form-item>
        <u-form-item label="面试职位" borderBottom ref="item1"><u--input disabled disabledColor="none" v-model="form.jobName" border="none"></u--input></u-form-item>
        <u-form-item label="岗位编号" borderBottom ref="item1"><u--input disabled disabledColor="none" v-model="form.jobNum" border="none"></u--input></u-form-item>
        <u-form-item label="面试日期" borderBottom @click="showDate = true" ref="item1">
          <u--input v-model="form.interviewTime" disabled disabledColor="#ffffff" border="none"></u--input>
          <u-icon slot="right" name="arrow-right"></u-icon>
        </u-form-item>
        <u-form-item label="毕业时间" borderBottom @click="showBiye = true" ref="item1">
          <u--input v-model="form.jobSeekerBySelfDto.graduationTime" disabled disabledColor="#ffffff" border="none"></u--input>
          <u-icon slot="right" name="arrow-right"></u-icon>
        </u-form-item>

        <u-form-item label="学历信息" borderBottom>
          <view v-show="form.jobSeekerBySelfDto.schoolProveUrl == null" class="uli" @click="chooseImage">
            <view class="uli" v-show="form.jobSeekerBySelfDto.schoolProveUrl !== null" v-for="(item,index) in urls" :key="index">
              <view class="miniBox" @click="form.schoolProveUrl = null">X</view>
              <image mode="aspectFill" :src="item"></image>
            </view>
          </view>
          
          <view class="headerPho" v-show="form.jobSeekerBySelfDto.schoolProveUrl !== null">
            <view class="miniBox" @click="form.jobSeekerBySelfDto.schoolProveUrl = null">X</view>
            <image :src="form.jobSeekerBySelfDto.schoolProveUrl" mode="" @click="clickImg"></image>
          </view>
        
        </u-form-item>
        
        <u-form-item label="使用推荐码" borderBottom>
          <u-checkbox-group iconPlacement="right" v-model="checkArr" @change="checkboxChange" placement="column">
            <u-checkbox :checked="checkNum === 1 ? 'true' : 'false'" shape="circle" size="20"></u-checkbox>
          </u-checkbox-group>
        </u-form-item>
        
        <u-form-item  v-if="whetherShow == true" label="推荐码" borderBottom><u--input v-model="codeObj.recommendedCode" border="none" trim></u--input></u-form-item>
        
        <u-form-item  v-if="whetherShow == true" label="推荐人姓名" borderBottom><u--input disabled disabledColor="#fff" v-model="codeObj.realName" border="none" trim></u--input></u-form-item>
      </u--form>

      <u-button type="primary" text="提交" customStyle="margin-top: 50px" @click="submit"></u-button>

      <u-datetime-picker :show="showDate" :value="dateTime" mode="date" closeOnClickOverly @confirm="dateTimechange" @cancel="dateClose" @close="dateClose"></u-datetime-picker>

      <u-datetime-picker
        :show="showBiye"
        :value="biyeTime"
        mode="date"
        closeOnClickOverly
        @confirm="dateBiyechange"
        @cancel="dateBiyeClose"
        @close="dateBiyeClose"
      ></u-datetime-picker>
    </view>

    <!-- 是否保存到我的信息的弹框 -->
    <view v-if="showMymessage" class="mymessage">
      <view class="showheader">提示</view>
      <view class="showtext">是否保存当前登记信息至个人中心</view>
      <view class="showbtns">
        <view class="btns" style="background-color: #02a7f0;" @click="getchange">确定</view>
        <view class="btns" style="background-color: #6fb83e;" @click="dongetchange">取消</view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      urls: [],
      imgList: '',
      userList: {}, //用户的基本信息
      checkArr: [],
      codeObj: {
        recommendedCode: '',
        realName: ''
      },
      checkNum: null,
      whetherShow:true,
      form: {
        interviewTime: '',
        isUpdate: 0,
        jobNum: '',
        jobSeekerBySelfDto: {
          avatarUrl: '',
          graduationTime: '',
          idCard: '',
          openId: '',
          realName: '',
          schoolProveUrl: '',
          telPhone: '',
          workNum: ''
        },
        jobTypeId: null,
        jobName: '',
        recruitmentType: 2,
        signUpType: 3
      },
      workdetails: {},
      showDate: false,
      jonposition: false,
      showCode: false,
      showBiye: false,
      dateTime: Number(new Date()),
      biyeTime: Number(new Date()),
      // postList: [], //岗位信息列表
      // actionsList: [],
      showMymessage: false,
      // 校验规则
      rules: {
        'jobSeekerBySelfDto.realName': {
          type: 'string',
          required: true,
          message: '请填写姓名',
          trigger: ['blur', 'change']
        },
        'jobSeekerBySelfDto.telPhone': {
          type: 'string',
          required: true,
          message: '请填写电话',
          trigger: ['blur', 'change']
        },
        'jobSeekerBySelfDtos.idCard': {
          type: 'string',
          required: true,
          message: '请填写身份证',
          trigger: ['blur', 'change']
        },
        'userInfo.sex': {
          type: 'string',
          required: true,
          message: '请填写岗位类别',
          trigger: ['blur', 'change']
        },
        'userInfo.code': {
          type: 'string',
          required: true,
          message: '请填写岗位编号',
          trigger: ['blur', 'change']
        },
        'userInfo.birthday': {
          type: 'string',
          required: true,
          message: '请选择日期',
          trigger: ['change']
        }
      }
    };
  },
  onLoad(options) {
    this.workdetails = JSON.parse(options.workDetails);
    console.log('options.workDetails9999', JSON.parse(options.workDetails));
    this.userList = uni.getStorageSync('userinfo');
    console.log('this.userList', this.userList);
    // this.getPostList()
    this.updateuserlist();
    this.getshuju();
  },
  onReady() {
  	this.checkNum = 1
  },
  methods: {
    // 给表单赋值用户基本信息
    updateuserlist() {
      this.form.jobSeekerBySelfDto.realName = this.userList.realName;
      this.form.jobSeekerBySelfDto.graduationTime = this.userList.graduationTime;
      this.form.jobSeekerBySelfDto.openId = this.userList.openId;
      this.form.jobSeekerBySelfDto.avatarUrl = this.userList.avatarUrl;
      this.form.jobSeekerBySelfDto.telPhone = this.userList.telPhone;
      this.form.jobSeekerBySelfDto.idCard = this.userList.idCard;
      this.form.jobSeekerBySelfDto.schoolProveUrl = this.userList.schoolProveUrl;
      this.form.jobSeekerBySelfDto.workNum = this.userList.workNum;
      this.form.jobTypeId = this.workdetails.jobType.id;
      this.form.jobName = this.workdetails.jobType.typeName;
      this.form.jobNum = this.workdetails.jobNum || this.workdetails.job.jobNum;
    },


// 获取分享人信息
    async getshuju() {
      console.log('uni.getStorageSync', uni.getStorageSync('scene')); 
      const str = uni.getStorageSync('scene');
      console.log('我是哦therOPENid' , str)
      
        const { data } = await uni.$http.get('/jobSeeker/findJobSeekerDetailByOpenId?scene=' + str+'&openId='+uni.getStorageSync('openid'));
        if (data.statusCode !== 200) {
          return;
        } else {
          if(data.data === null) {
            return this.checked = false
          }else {
             this.checked = true
            this.checkNum = 0
            this.codeObj.recommendedCode = data.data.recommendedCode;
            this.codeObj.realName = data.data.realName;
          }
         
        }
     
    },
    // 复选框是否选中
    checkboxChange(e) {
      this.checkNum = e.length;
      console.log(this.checkNum);
      if(this.checkNum === 1) {
        this.whetherShow = true
      }else {
        this.whetherShow = false
      }
    },
    dateClose() {
      this.showDate = false;
      this.$refs.formRef.validateField('userInfo.time');
    },
    dateTimechange(e) {
      this.showDate = false;
      this.form.interviewTime = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
      this.$refs.formRef.validateField('userInfo.time');
    },
    dateBiyeClose() {
      this.showBiye = false;
      this.$refs.formRef.validateField('userInfo.birthday');
    },
    dateBiyechange(e) {
      this.showBiye = false;

      this.form.jobSeekerBySelfDto.graduationTime = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
      this.$refs.formRef.validateField('userInfo.birthday');
    },
    chooseImage() {
      var that = this;
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], //original 原图，compressed 压缩图
        sourceType: ['album', 'camera'], //album 从相册选图，camera 使用相机
        success: function(res) {
          console.log('我是res', JSON.stringify(res.tempFilePaths));
          const tempFilePaths = res.tempFilePaths;
          console.log('tempFilePaths', tempFilePaths);
          that.urls = tempFilePaths;
          uni.uploadFile({
            url: uni.$http.baseUrl + '/upload/uploadPic',
            filePath: that.urls[0],
            header: {
              token: uni.getStorageSync('tokenValue')
            },
            name: 'file',
            success(uploadFileRes) {
              // console.log('uploadFileRes.data===>' , JSON.parse(uploadFileRes.data));
              const photores = JSON.parse(uploadFileRes.data);
              console.log('uploadFileRes.data===>', photores);
              if (photores.statusCode !== 200) {
                return;
              } else {
                that.form.jobSeekerBySelfDto.schoolProveUrl = photores.data;
              }

              console.log('wowowowwo', that.form);
            }
          });
        }
      });
    },
    clickImg(){
      uni.previewImage({
      					urls: [this.form.jobSeekerBySelfDto.schoolProveUrl], //需要预览的图片http链接列表，多张的时候，url直接写在后面就行了
      					//current: '', // 当前显示图片的http链接，默认是第一个
      					success: function(res) {},
      					fail: function(res) {},
      					complete: function(res) {},
      				})
    },
 //提交的请求
  async tijiao(){
   const { data } = await uni.$http.post('/sign/signUpAddBySelf', this.form);
   console.log('9999999', data);
   if (data.statusCode !== 200) {
     uni.showToast({
       title: data.message,
       icon: 'none'
     });
   } else {
     uni.showToast({
       title: data.message,
       icon: 'success'
     });
     setTimeout(function() {
       uni.navigateBack();
     }, 700);
   }
 },
    // 提交
     submit() {
      console.log(this.form);
      // this.form.jobTypeId = this.workdetails.jobType.id
      const objone = {
        avatarUrl: this.userList.avatarUrl, //头像地址
        graduationTime: this.userList.graduationTime, //毕业时间
        idCard: this.userList.idCard, //身份证
        openId: this.userList.openId,
        realName: this.userList.realName, //姓名
        schoolProveUrl: this.userList.schoolProveUrl, //学历证明
        telPhone: this.userList.telPhone, //电话
        workNum: this.userList.workNum //工号
      };
      if (JSON.stringify(this.form.jobSeekerBySelfDto) === JSON.stringify(objone)) {
       if (this.checkNum === 1) {
         this.form.recommendedCode = this.codeObj.recommendedCode;
         this.tijiao();
       } else {
         this.form.recommendedCode = '';
         this.tijiao();
       }
      } else {
        this.showMymessage = true;
      }
    },
    // 更新用户信息
    async getchange() {
      this.showMymessage = false;
      this.form.isUpdate = 1;

      console.log('图片为啥上传不了', this.form.jobSeekerBySelfDto);
      
      if (this.checkNum === 1) {
        this.form.recommendedCode = this.codeObj.recommendedCode;
      } else {
        this.form.recommendedCode = '';
      }
      const { data } = await uni.$http.post('/sign/signUpAddBySelf', this.form);
      if (data.statusCode !== 200) {
        uni.showToast({
          title: data.message,
          icon: 'none'
        });
      } else {
        uni.setStorageSync('userinfo', this.form.jobSeekerBySelfDto);

        uni.showToast({
          title: data.message,
          icon: 'success'
        });
        setTimeout(function() {
          uni.navigateBack();
        }, 700);
      }
    },
    // 不更新用户提交
    async dongetchange() {
      this.form.isUpdate = 0;
      this.showMymessage = false;
      
      if (this.checkNum === 1) {
        this.form.recommendedCode = this.codeObj.recommendedCode;
      } else {
        this.form.recommendedCode = '';
      }
      const { data } = await uni.$http.post('/sign/signUpAddBySelf', this.form);
      if (data.statusCode !== 200) {
        uni.showToast({
          title: data.message,
          icon: 'none'
        });
      } else {
        uni.showToast({
          title: data.message,
          icon: 'success'
        });
        setTimeout(function() {
          uni.navigateBack();
        }, 700);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.content {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #ffffff;

  .content-header {
    // height: 500rpx;
    image {
      width: 100%;
      height: 350rpx;
    }
  }

  .content-form {
    position: fixed;
    top: 5%;
    left: 10%;
    padding: 20rpx 40rpx;
    box-sizing: border-box;
    width: 80%;
    border-radius: 30rpx;
    background-color: #ffffff;

    input {
      height: 60rpx;
      font-size: 20rpx;
    }

    /deep/.u-form-item__body__left__content__label.data-v-5e7216f1 {
      font-size: 24rpx;
    }

    .u-form-item__body.data-v-5e7216f1 {
      padding: 5px;
    }
  }

  /deep/.u-form-item {
    image {
      width: 100rpx;
      height: 100rpx;
    }
  }
  .headerPho {
    position: relative;
    width: 100rpx;
    height: 100rpx;
    .miniBox {
      position: absolute;
      top: -5px;
      right: -5px;
      width: 30rpx;
      height: 30rpx;
      font-size: 18rpx;
      text-align: center;
      line-height: 30rpx;
      color: #ffffff;
      background-color: #373737;
      border-radius: 50%;
    }
    image {
      width: 100%;
      height: 100%;
    }
  }

  .uli {
    position: relative;
    width: 100rpx;
    height: 100rpx;
    background: url(https://small.qaqavr.com/img/zhongrui/upload.png) no-repeat;
    background-size: contain;
 .miniBox {
      position: absolute;
      top: -5px;
      right: -5px;
      width: 30rpx;
      height: 30rpx;
      font-size: 18rpx;
      text-align: center;
      line-height: 30rpx;
      color: #ffffff;
      background-color: #373737;
      border-radius: 50%;
    }
    image {
      width: 100%;
      height: 100%;
    }
  }

  .mymessage {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 500rpx;
    height: 300rpx;
    border-radius: 30rpx;
    background-color: #ffffff;
    overflow: hidden;
    box-shadow: 0 0 5rpx #ccc;

    .showheader {
      height: 80rpx;
      background-color: #e8e8e8;
      font-size: 38rpx;
      text-align: center;
      line-height: 80rpx;
    }

    .showtext {
      margin-top: 20rpx;
      height: 80rpx;
      background-color: #ffffff;
      text-align: center;
      font-size: 24rpx;
      line-height: 80rpx;
    }

    .showbtns {
      margin-top: 20rpx;
      height: 80rpx;
      display: flex;
      padding: 0 20rpx;
      background-color: #ffffff;
      justify-content: space-around;

      .btns {
        width: 150rpx;
        height: 50rpx;
        border-radius: 5rpx;
        text-align: center;
        line-height: 50rpx;
        color: #ffffff;
      }
    }
  }
}
</style>
